'''
Created on Nov 29, 2010

@author: dexen
'''

import sys
import traceback

import dexen

import conds
from my_ind import MyInd
from slave_tasks.dev import DevTask
from slave_tasks.eval import EvalDaylightTask, EvalEnergyTask, EvalPrivacyTask
from slave_tasks.feedback import FeedbackTask

FEED_SIZE = 20
POP_SIZE = FEED_SIZE * 5
MAX_BIRTH = 10000


class MyMasterTask(dexen.MasterTask):
    def __init__(self, master_env):
        try:
            print "I am initializing in mastertask"
            assert isinstance(master_env, dexen.MasterEnv)
            master_env.start_task(DevTask, "Development", 1, conds.dev_cond)
            master_env.start_task(EvalEnergyTask, "Eval Energy", 1, conds.energy_cond)
            master_env.start_task(EvalDaylightTask, "Eval Daylight", 1, conds.daylight_cond)
            master_env.start_task(EvalPrivacyTask, "Eval Privacy", 1, conds.privacy_cond)
            master_env.start_task(FeedbackTask, "Feedback", FEED_SIZE, conds.feedback_cond)
            print "I have started all the tasks"
        except:
            print "MasterTask: Exception"
            traceback.print_tb(sys.exc_info()[2])
        
        for _ in range(POP_SIZE):
            ind = MyInd()
            #print "MasterTask: ", ind
            master_env.set_ind(ind)    
    
    def __execute__(self, master_env):
        print "I am executing master task"
        assert isinstance(master_env, dexen.MasterEnv)
        pop_size = master_env.num_inds()
        print "MasterTask: pop size is ", pop_size
        
        if pop_size == MAX_BIRTH:
            master_env.stop_master()
    
